home *** CD-ROM | disk | FTP | other *** search
- Path: gate.net!pslfl2-21
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c
- Subject: Re: Help a fellow C programer
- Date: 13 Jan 1996 10:19:50 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4d8106$1s4g@news.gate.net>
- References: <4d3t4q$ea6@klein.delphi.com> <4d71td$88a@info.uah.edu>
- NNTP-Posting-Host: pslfl2-21.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4d71td$88a@info.uah.edu>, gbacon@oreo (Greg Bacon) wrote:
- >Diane Barker (Barker@mci.newscorp.com) wrote:
- >: Can any one tell me the C function/statment that is equal to the math
- >: function ! (factorial).
- >
- >: Email me at: BARKER@MCI.NEWSCORP.COM if you can help
- >
- >: Mike Barker
- >
- >int factorial(unsigned int a)
- >{
- > if ((a == 0) || (a == 1))
- > return 1;
- > else
- > return a * factorial(a-1);
- >}
- >
- >That's a recursive version. a is unsigned since the factorial operation
- >is only defined over [0, \infinity)
- >
- >Greg
-
- This is a non-recursive more robust version.
-
- unsigned long factorial(unsigned a)
- {
- unsigned i=1;
- unsigned long retval=1;
-
- if(a>12) /*overflow*/
- return 0L;
- do {
- retval *= i++;
- } while(i<=a);
- return retval;
- }
-
- Bill
-
- "Whatcha got on?...Your mind?"
-